package andev.schoolhelper.apputil;

import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class QueryCourseUtil {
    public static void main(String[] args) {
        File in = new File("d:\\b.txt");
        Document document = null;
        try {
            document = Jsoup.parse(in, "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        QueryCourseUtil x = new QueryCourseUtil();
        x.parseCourses(document.toString());
		/*for (Element element : elements)
		{
			String td=element.toString();
			x.o(td+day);
			if (td.contains("&nbsp;"))
			{
			}
			else
			{
				Pattern pattern=Pattern.compile(">.+<br");
				Matcher matcher=pattern.matcher(td);
				JSONObject course=new JSONObject();
				try
				{
					course.put("day", Integer.toString(day));
					course.put("sectionStart", Integer.toString(sectionStart));
					x.o(day+":"+sectionStart);
				}
				catch (JSONException e)
				{
					e.printStackTrace();
				}
				while (matcher.find())
				{
					String group=matcher.group();
					group=group.replaceAll("[><br]", "");
					if (!group.equals(""))
					{
						x.o(group);

					}
				}
			}

			day++;
			if (day==8)
			{
				day=1;
				sectionStart++;
			}
		}*/
    }

    public ArrayList<JSONObject> parseCourses(String html) {
        Document document = Jsoup.parse(html);
        Elements elements = document.select("table[id=Table1]");
        document = Jsoup.parse(elements.toString());
        elements = document.select("tr");
        ArrayList<JSONObject> trWithCourses = new ArrayList<JSONObject>();
        for (Element element : elements) {
            String tr = "<table>" + element.toString() + "</table>";
            //第一个tr是表格时间的标题
            if (tr.contains("时间") || tr.contains("早晨")) {
                continue;
            }
            trWithCourses.addAll(parseTR(tr));
        }
        /*for (JSONObject course : trWithCourses)
		{
			try
			{
				o(course.getInt("day")+":"+course.getInt("sectionA")+":"+course.getInt("rowspan")+":"+course.getString("coursename")+":"+course.getString("teacher")+":"+course.getString("classroom"));
			}
			catch (JSONException e)
			{
				e.printStackTrace();
			}
		}*/
        return trWithCourses;
    }

    public void o(String a) {
        System.out.println(a);
    }

    private ArrayList<JSONObject> parseTR(String tr) {
        ArrayList<JSONObject> tdWithCourses = new ArrayList<JSONObject>();
        Document document = Jsoup.parse(tr);
        Elements elements = document.select("td");
        int day = 0, sectionA = 0;
        for (Element element : elements) {
            String td = element.toString();
            if (sectionA != 0) {
                day++;
                JSONObject tdWithCourse = getCourse(td, day, sectionA);
                if (tdWithCourse != null) {
                    tdWithCourses.add(tdWithCourse);
                }
                continue;
            }
            sectionA = getSectionStart(td);
        }
        return tdWithCourses;
    }

    private JSONObject getCourse(String td, int day, int sectionA) {
        JSONObject tdWithCourse = null;
        if (td.contains("&nbsp;") || !td.contains("rowspan")) {
            return null;
        }
        int rowspan = 0;
        Pattern pattern = Pattern.compile("rowspan=\"(.)\"");
        Matcher matcher = pattern.matcher(td);
        if (matcher.find()) {
            rowspan = stringNumberToInt(matcher.group(1));
            tdWithCourse = new JSONObject();
            try {
                tdWithCourse.put("day", day);
                tdWithCourse.put("sectionA", sectionA);
                tdWithCourse.put("rowspan", rowspan);
                tdWithCourse.put("coursename", getCourseName(td));
                tdWithCourse.put("teacher", getTeacher(td));
                tdWithCourse.put("classroom", getClassroom(td));
                tdWithCourse.put("week", getWeek(td));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return tdWithCourse;
    }

    private String getWeek(String td) {
        Pattern pattern = Pattern.compile("\\{(.{0,10})\\}");
        Matcher matcher = pattern.matcher(td);
        String week = "0-0,0";
        StringBuffer weekBuffer = null;
        if (matcher.find()) {
            week = matcher.group(1);
            if (week.contains("第")) {
                week = week.replace("第", "");
                if (week.contains("|")) {
                    if (week.contains("单")) {
                        week = week.replace("周|单周", "");
                        weekBuffer = new StringBuffer(week);
                        weekBuffer.append(",1");
                    } else {
                        week = week.replace("周|双周", "");
                        weekBuffer = new StringBuffer(week);
                        weekBuffer.append(",2");
                    }
                } else {
                    week = week.replace("周", "");
                    weekBuffer = new StringBuffer(week);
                    weekBuffer.append(",0");
                }
            } else {
                weekBuffer = new StringBuffer("undefined");
            }
            week = weekBuffer.toString();
        }
        return week;
    }

    private String[] getCourseInfo(String td) {
        Pattern pattern = Pattern.compile(">(.+)</td>");
        Matcher matcher = pattern.matcher(td);
        if (matcher.find()) {
            td = matcher.group(1);
        }
        String[] courseInfo = td.split("<br>");
        return courseInfo;
    }

    private String getClassroom(String td) {
        String[] courseInfo = getCourseInfo(td);
        if (courseInfo.length == 3) {
            return "";
        }
        String classroom = courseInfo[3];
        return classroom;
    }

    private String getTeacher(String td) {
        String teacher = getCourseInfo(td)[2];
        return teacher;
    }

    private String getCourseName(String td) {
        String courseName = getCourseInfo(td)[0];
        return courseName;
    }

    public int getSectionStart(String td) {
        int sectionStart = 0;
        Pattern pattern = Pattern.compile(">第(.+)节<");
        Matcher matcher = pattern.matcher(td);
        if (matcher.find()) {
            sectionStart = stringNumberToInt(matcher.group(1));
        }
        return sectionStart;
    }

    public int stringNumberToInt(String string) {
        int x = 0;
        switch (string) {
            case "一":
            case "1":
                x = 1;
                break;
            case "二":
            case "2":
                x = 2;
                break;
            case "三":
            case "3":
                x = 3;
                break;
            case "四":
            case "4":
                x = 4;
                break;
            case "五":
            case "5":
                x = 5;
                break;
            case "六":
            case "6":
                x = 6;
                break;
            case "七":
            case "7":
                x = 7;
                break;
            case "八":
            case "8":
                x = 8;
                break;
            case "九":
            case "9":
                x = 9;
                break;
            case "十":
            case "10":
                x = 10;
                break;
            case "11":
                x = 11;
                break;
            case "12":
                x = 12;
                break;
            case "13":
                x = 13;
                break;
            case "14":
                x = 14;
                break;
        }
        return x;
    }
}
